package id.co.rho.scoring.cid.client.scaffold;

import id.co.rho.scoring.cid.client.command.CommandDispatcherAsync;
import id.co.rho.scoring.cid.client.managed.request.ApplicationRequestFactory;
import id.co.rho.scoring.cid.client.mvc.AppEvents;
import id.co.rho.scoring.cid.client.mvc.AppView;
import id.co.rho.scoring.cid.client.mvc.NavigationView;
import id.co.rho.scoring.cid.client.mvc.compare.CompareModelView;
import id.co.rho.scoring.cid.client.mvc.login.LoginView;
import id.co.rho.scoring.cid.client.mvc.model.ModelSearchView;
import id.co.rho.scoring.cid.client.mvc.score.UploadAnswerView;
import id.co.rho.scoring.cid.client.mvc.scoringmodel.ScoringModelEditView;
import id.co.rho.scoring.cid.client.mvc.scoringmodel.ScoringModelView;
import id.co.rho.scoring.cid.client.mvc.typemodelconfig.TypeModelConfigView;
import id.co.rho.scoring.cid.client.mvc.typemodelconfigsearch.TypeModelConfigSearchView;
import id.co.rho.scoring.cid.client.mvc.upload.UploadModelView;
import id.co.rho.scoring.cid.client.mvc.versioning.VersioningModelView;
import id.co.rho.scoring.cid.client.mvc.versioning.VersioningSetDefaultView;
import id.co.rho.scoring.cid.client.utils.LoginCheck;

import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.inject.Inject;

/**
 * Application for browsing entities.
 */
public class ScaffoldExtDesktopApp extends ScaffoldApp {
    private Dispatcher dispatcher;

    @Inject
    public ScaffoldExtDesktopApp(ApplicationRequestFactory requestFactory,
                                 final CommandDispatcherAsync commandDispatcherAsync,
                                 AppView appView,
                                 NavigationView nv,
                                 LoginView loginView,
                                 LoginCheck loginCheck,
                                 TypeModelConfigView typeModelConfigView,
                                 TypeModelConfigSearchView typeModelConfigSearchView,
                                 UploadModelView uploadModelView,
                                 ScoringModelView scoringModelView,
                                 ScoringModelEditView scoringModelEditView,
                                 ModelSearchView modelSearchView,
                                 UploadAnswerView uploadAnswerView,
                                 CompareModelView compareModelView,
                                 VersioningModelView versioningModelView,
                                 VersioningSetDefaultView versioningSetDefaultView) {
        dispatcher = Dispatcher.get();

        // Register Controller
        dispatcher.addController(appView.getController());
        dispatcher.addController(nv.getController());
        
        dispatcher.addController(loginView.getController());
        
        dispatcher.addController(uploadModelView.getController());
        dispatcher.addController(typeModelConfigView.getController());
        dispatcher.addController(typeModelConfigSearchView.getController());
        dispatcher.addController(modelSearchView.getController());
        dispatcher.addController(scoringModelView.getController());
        dispatcher.addController(scoringModelEditView.getController());
        dispatcher.addController(uploadAnswerView.getController());
        dispatcher.addController(compareModelView.getController());
        dispatcher.addController(versioningModelView.getController());
        dispatcher.addController(versioningSetDefaultView.getController());
        dispatcher.dispatch(AppEvents.Init);
        loginCheck.checkCookie();
        generateTimer();

    }

    public void generateTimer() {
        Timer t = new Timer() {
            public void run() {
            }
        };
        t.scheduleRepeating(30 * 1000);
    }

    public void run() {
        /* Hide the loading message */
        Element loading = Document.get().getElementById("loading");
        loading.getParentElement().removeChild(loading);
    }

    public static boolean isExplorer() {
        String test = Window.Location.getPath();
        if (test.indexOf("pages") != -1) {
            return false;
        }
        return true;
    }
}
